//
//  ZLoadView.m
//  OC-weibo
//
//  Created by zero on 17/3/20.
//  Copyright © 2017年 zero. All rights reserved.
//

#import "ZLoadView.h"

@interface ZLoadView()

//  圈圈
@property (nonatomic,weak)UIImageView *cicrlView;

//  遮罩
@property (nonatomic,weak)UIImageView *coverView;

//  房子
@property (nonatomic,weak)UIImageView *hourseView;

//  文字
@property (nonatomic,weak)UILabel *titleLabel;

@property (nonatomic,weak)CABasicAnimation *baseAnimation;

@property (nonatomic,weak)UIButton *registeBtn;
@property (nonatomic,weak)UIButton *loginBtn;

@end

@implementation ZLoadView

#pragma mark
#pragma mark - 重写init方法
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        //  布局
        [self setupUI];
        
        //  动画
        [self addBaseAnimation];
    }
    
    return self;
}

#pragma mark
#pragma mark - 布局
- (void)setupUI{
    
    //  实例化圈圈
    UIImageView *cicrlView = [[UIImageView alloc] init];
    
    self.cicrlView = cicrlView;
    
    cicrlView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
    
    [self addSubview:cicrlView];
    
    //  约束
    [cicrlView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(-80);
        
    }];
    
    //  实例化遮罩
    UIImageView *coverView = [[UIImageView alloc] init];
    
    self.coverView = coverView;
    
    coverView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
    [self addSubview:coverView];
    
    //  约束
    [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(cicrlView);
    }];

    //  实例化房子
    UIImageView *hourseView = [[UIImageView alloc] init];
    
    self.hourseView = hourseView;
    
    hourseView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
    [self addSubview:hourseView];
    
    //  约束
    [hourseView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(cicrlView);
        
    }];

    //  实例化标题
    UILabel *titleLabel = [UILabel z_labelWithText:@"哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈" andFontSize:14 andColor:[UIColor lightGrayColor]];
    
    self.titleLabel = titleLabel;
    titleLabel.preferredMaxLayoutWidth = 200;
    titleLabel.numberOfLines = 0;
    
    [self addSubview:titleLabel];
    
    //  约束

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(cicrlView);
        make.top.equalTo(coverView.mas_bottom).offset(8);
    }];

    //  实例化注册
    UIButton *registeBtn = [UIButton z_buttonWithNormalImageName:@"common_button_white" andHighLightedImageName:nil andSelectedImageName:nil andTitle:@"注册" andFrame:CGRectZero];
    
    self.registeBtn = registeBtn;
    [registeBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    [self addSubview:registeBtn];
    
    //  约束
    [registeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(titleLabel);
        make.top.equalTo(titleLabel.mas_bottom).offset(8);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];

    //  实例化登录
    UIButton *loginBtn = [UIButton z_buttonWithNormalImageName:@"common_button_white" andHighLightedImageName:nil andSelectedImageName:nil andTitle:@"登录" andFrame:CGRectZero];
    
    self.loginBtn = loginBtn;
    [loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:loginBtn];
    
    //  约束
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(titleLabel);
        make.top.equalTo(registeBtn);
        
        make.size.equalTo(registeBtn);
    }];
    
    
    //  添加点击方法
    
    [loginBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [registeBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark
#pragma mark - 监听点击方法
- (void)clickBtn:(UIButton *)sender{
    
    if (sender == self.registeBtn) {
        
        if (self.registeBlock) {
            
            self.registeBlock(self);
        }
    }else{
        if (self.loginBlock) {
            
            self.loginBlock(self);
        }
    }
}
#pragma mark
#pragma mark - 添加基本动画
- (void)addBaseAnimation{
    
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    self.baseAnimation = baseAnimation;
    
    baseAnimation.toValue = @(2.0 * M_PI);
    baseAnimation.duration = 15;
    baseAnimation.repeatCount = MAXFLOAT;
    baseAnimation.removedOnCompletion = NO;
    
    [self.cicrlView.layer addAnimation:baseAnimation forKey:nil];
}
#pragma mark
#pragma mark - 设置图片文字
- (void)setImage:(NSString *)imageName andTitle:(NSString *)title{
    
    self.cicrlView.image = [UIImage imageNamed:imageName];
    self.titleLabel.text = title;
    
    self.coverView.hidden = YES;
    self.hourseView.hidden = YES;
    
    [self.cicrlView.layer removeAllAnimations];
    
}
@end
